/*******************************************************************************
 * This file is part of Crunch Graphics.
 *
 * Crunch Graphics is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * Crunch Graphics is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * Crunch Graphics.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

package com.crunch.graphics.gpu;
import javax.media.opengl.GL;

/** Encapsulates a renderbuffer on the GPU.
 */
public class GpuRenderbuffer extends ReferenceCountedGpuObject {
	/** Description used to initialize a renderbuffer.
	 */
	public static class RenderbufferDescription {
		public int internalFormat;          /**< The renderbuffer's internal format.*/
		public int samples;                 /**< The number of samples per pixel.*/
		public int width;                   /**< The width of the renderbuffer.*/
		public int height;                  /**< The height of the renderbuffer.*/

		public RenderbufferDescription() {  /**< Sets default values.*/
			internalFormat = GL.GL_RGBA8;
			samples = 0;
			width = 0;
			height = 0;
		}
	}

	/** Returns the underlying renderbuffer ID.
	 */
	public int getId() {
		return id;
	}

	/** Returns the renderbuffer's internal format.
	 */
	public int getInternalFormat() {
		return internalFormat;
	}

	/** Returns the number of samples in the renderbuffer.
	 */
	public int getSamples() {
		return samples;
	}

	/** Returns the renderbuffer size in the given dimension.
	 */
	public int getSize(int dimension) {
		return dimension;
	}

	/** Returns the renderbuffer width.
	 */
	public int getWidth() {
		return width;
	}

	/** Returns the renderbuffer height.
	 */
	public int getHeight() {
		return height;
	}

	/** Binds the renderbuffer.
	 */
	public void bind() {
		getGpuState().bindRenderbuffer(this);
	}

	GpuRenderbuffer(GpuState state, RenderbufferDescription desc) {
		super(state);
		internalFormat = desc.internalFormat;
		samples = desc.samples;
		width = desc.width;
		height = desc.height;

		int[] buf = new int[1];
		getGpuState().getGl().glGenRenderbuffers(1, buf, 0);
		id = buf[0];
		bind();
		getGpuState().getGl().glRenderbufferStorageMultisample(GL.GL_RENDERBUFFER, samples, internalFormat, width, height);
	}

	protected void onRelease() {
		getGpuState().getGl().glDeleteRenderbuffers(1, new int[] { id }, 0);
	}

	private int id;             // internal ID
	private int internalFormat; // internal format
	private int samples;        // number of samples
	private int width, height;  // size
}